package com.huadao.cart_ccs.common.inputControl;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

public class MaxNumberTextWatch implements TextWatcher {

    private int max;
    private EditText editText;
    //当超过最大值时，是否自动变为最大值
    private boolean isAutoMax;
    private NumberMaxAttention numberMaxAttention;

    public MaxNumberTextWatch(int max) {
        this.max = max;
    }

    public MaxNumberTextWatch(int max, EditText editText, boolean isAutoMax) {
        this.max = max;
        this.editText = editText;
        this.isAutoMax = isAutoMax;
    }

    public MaxNumberTextWatch(int max, EditText editText, boolean isAutoMax, NumberMaxAttention numberMaxAttention) {
        this.max = max;
        this.editText = editText;
        this.isAutoMax = isAutoMax;
        this.numberMaxAttention = numberMaxAttention;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (s.length() == 0) {
            return;
        }
        try {
            int number=Integer.parseInt(s.toString());
            if (number > max) {
                if(isAutoMax) {
                    String maxStr = String.valueOf(max);
                    s.clear();
                    s.append(maxStr);
                }
                if(numberMaxAttention!=null){
                    numberMaxAttention.onNumberMax(String.valueOf(max),editText);
                }
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
    }

}